/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年4月13日 下午2:51:49
 */
package com.absir.appserv.master.service;

import java.io.Serializable;
import java.nio.channels.SocketChannel;

import com.absir.appserv.system.service.MasterSessionResolverImpl;
import com.absir.bean.basis.Base;
import com.absir.bean.inject.value.Bean;
import com.absir.server.socket.IServerContext;
import com.absir.server.socket.ServerContext;
import com.absir.server.socket.SocketChannelContext;
import com.absir.server.socket.SocketReceiverContext;

/**
 * @author absir
 *
 */
@Base
@Bean
public class MasterSessionResolver extends MasterSessionResolverImpl {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.MasterSessionResolverImpl#register(java
	 * .lang.String, java.lang.String[], java.nio.channels.SocketChannel,
	 * com.absir.server.socket.ServerContext, byte[])
	 */
	@Override
	protected ServerContext register(String id, String[] params, SocketChannel socketChannel, ServerContext serverContext,
			byte[] buffer) throws Throwable {
		ServerContext context = super.register(id, params, socketChannel, serverContext, buffer);
		MasterSlaveService.ME.registerSlave(id, params, socketChannel);
		return context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.MasterSessionResolverImpl#unRegister
	 * (java.io.Serializable, java.nio.channels.SocketChannel,
	 * com.absir.server.socket.ServerContext,
	 * com.absir.server.socket.SocketChannelContext)
	 */
	@Override
	public void unRegister(Serializable id, SocketChannel socketChannel, IServerContext serverContext,
			SocketChannelContext socketChannelContext) throws Throwable {
		if (socketChannelContext != null) {
			super.unRegister(id, socketChannel, serverContext, socketChannelContext);
			if (id != null && id != SocketReceiverContext.UN_REGISTER_ID && id instanceof String) {
				MasterSlaveService.ME.unRegisterSlave((String) id);
			}
		}
	}
}
